package com.briup.demo.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.briup.demo.bean.XcdGoods;
import com.briup.demo.common.Response;
import com.briup.demo.service.XcdGoodsService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "商品信息管理")
@RestController
@RequestMapping("/goods")
public class XcdGoodsController {

	@Autowired
	private XcdGoodsService goodsService;
	
	@ApiOperation(value = "获取所有商品信息",notes = "从数据库中检索所有商品信息返回")
	@GetMapping("/getAllGoods")
	public Response<List<XcdGoods>> getAll(){
		List<XcdGoods> goods = goodsService.getAllGoods();
		return Response.ok(goods);
	}
	
	@ApiOperation(value = "保存或更新商品信息",notes = "新增商品时，用户可自定义输入商品编号，如果用户没有"
			+ "输入编号，则系统会帮忙生成一个编号")
	@PostMapping("/saveOrUpdate")
	public Response<String> saveOrUpdate(XcdGoods goods){
		int num = goodsService.saveOrUpdate(goods);
		if(num == 1) {
			return Response.ok("插入信息成功");
		}else {
			return Response.ok("更新信息成功");
		}

	}
	
	@ApiOperation(value = "按照商品编号删除商品信息")
	@DeleteMapping("/deleteById")
	public Response<String> deleteById(String id){
		int num = goodsService.deleteById(id);
		if(num == 1) {
			return Response.ok("删除成功");		
		}else {
			return Response.ok("删除失败");
		}
	}
	
	@ApiOperation(value = "按商品编号查询商品信息")
	@GetMapping("/selectById")
	public Response<XcdGoods> selectById(String id){
		XcdGoods xg = goodsService.selectById(id);
		return Response.ok(xg);
	}
	
	@ApiOperation(value = "按商品名查询商品信息")
	@GetMapping("/selectByName")
	public Response<XcdGoods> selectByName(String name){
		XcdGoods xg = goodsService.selectByName(name);
		return Response.ok(xg);
	}
	
	
	@ApiOperation(value = "批量删除商品信息")
	@DeleteMapping("/deleteByIds")
	public Response<String> deleteByIds(String[] ids){
		int num = goodsService.deleteByIds(ids);
		System.out.println("删除了"+num+"条信息");
		if(num == 0){
			return Response.ok("删除失败");
		}else {
			return Response.ok("删除成功");
		}
	}
	
	
	
}
